package ecmnext.client;

/***************************************************************/
/*                     SOFTWARE SECTION                        */
/***************************************************************/
/*
 * <p>Name: ECMNext</p>
 * <p>Description: Simulation of the platform of trading Software</p>
 * <p>Licence: General Public License</p>
 * <p>Organisation: EC Marseille</p>
 * <p>Persons involved in the project : groupe Plateforme de Trading</p>
 * <p>Current version: 1.0</p>
 */

/***************************************************************/
/*                       IMPORT SECTION                        */
/***************************************************************/

import org.jdom.*;
import ecmnext.business.*;
import ecmnext.client.event.*;
import ecmnext.net.*;
import ecmnext.utils.*;

/***************************************************************/
/*                DataManager CLASS SECTION                    */
/***************************************************************/
/**
 * <p>Title : DataManager</p>
 * <p>Description : </p>
 * @author Xuewen YANG ET Antoine COUDERC
 * @version 1.0
 */

public class DataManager implements NetworkListener{

  public DataManager() {
    ClientCore.addNetworkListener(this, "Institution");
    ClientCore.addNetworkListener(this, "Portfolio");
    ClientCore.addNetworkListener(this, "OperatorPlayed");
  }

  public void objectReceived(Document doc) {
    if (doc.getRootElement().getName().equals("Portfolio")) {
      ClientCore.getPortfolio().initFromNetworkInput(doc.getRootElement());
    }
    else if (doc.getRootElement().getName().equals("OperatorPlayed")) {
      OperatorPlayed opPlayed = new OperatorPlayed("");
      if (opPlayed.initFromNetworkInput(doc.getRootElement())) {
        Institution inst = ClientCore.getInstitution(opPlayed.getInstitutionName());
        if (inst != null) {
          Operator op = inst.getOperator(opPlayed.getOperatorName());
          if (op != null) {
            ClientCore.addOperatorPlayed(op);
          }
          else {
            Utils.logger.warn("The operator has not been found on the institution given. (" +
                              opPlayed.getInstitutionName() + ", " +
                              opPlayed.getOperatorName() + ")");
          }
        }
        else {
          Utils.logger.warn("Operator plays on an institution we did not have. (" +
                            opPlayed.getInstitutionName() + ", " +
                            opPlayed.getOperatorName() + ")");
        }
      }
    }
    else if (doc.getRootElement().getName().equals("Institution")) {
      Institution instit = Institution.loadInstitutionFromXml(doc.getRootElement());
      ClientCore.addInstitution(instit);
    }
  }
}
